package com.marketingcenter.controller;

import com.marketingcenter.entity.CountCard;
import com.marketingcenter.service.CountCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 次数权益卡控制器
 */
@RestController
@RequestMapping("/count-cards")
public class CountCardController {

    @Autowired
    private CountCardService countCardService;

    /**
     * 创建次数权益卡
     */
    @PostMapping
    public ResponseEntity<CountCard> create(@RequestBody CountCard card) {
        card.setType(CountCard.PromotionType.COUNT_CARD);
        CountCard createdCard = countCardService.create(card);
        return ResponseEntity.ok(createdCard);
    }

    /**
     * 更新次数权益卡
     */
    @PutMapping("/{id}")
    public ResponseEntity<CountCard> update(@PathVariable Long id, @RequestBody CountCard card) {
        card.setId(id);
        CountCard updatedCard = countCardService.update(card);
        return ResponseEntity.ok(updatedCard);
    }

    /**
     * 删除次数权益卡
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        countCardService.delete(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据ID查询次数权益卡
     */
    @GetMapping("/{id}")
    public ResponseEntity<CountCard> getById(@PathVariable Long id) {
        CountCard card = countCardService.findById(id);
        return card != null ? ResponseEntity.ok(card) : ResponseEntity.notFound().build();
    }

    /**
     * 查询所有次数权益卡
     */
    @GetMapping
    public ResponseEntity<List<CountCard>> getAll() {
        List<CountCard> cards = countCardService.findAll();
        return ResponseEntity.ok(cards);
    }

    /**
     * 根据状态查询次数权益卡
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<CountCard>> getByStatus(@PathVariable Integer status) {
        List<CountCard> cards = countCardService.findByStatus(status);
        return ResponseEntity.ok(cards);
    }

    /**
     * 查询活动中的次数权益卡
     */
    @GetMapping("/active")
    public ResponseEntity<List<CountCard>> getActive() {
        List<CountCard> cards = countCardService.findActivePromotions();
        return ResponseEntity.ok(cards);
    }

    /**
     * 启用次数权益卡
     */
    @PutMapping("/{id}/enable")
    public ResponseEntity<Void> enable(@PathVariable Long id) {
        countCardService.enable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 停用次数权益卡
     */
    @PutMapping("/{id}/disable")
    public ResponseEntity<Void> disable(@PathVariable Long id) {
        countCardService.disable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 用户购买次数权益卡
     */
    @PostMapping("/{id}/purchase")
    public ResponseEntity<Boolean> purchase(@PathVariable Long id, @RequestParam Long userId) {
        boolean success = countCardService.purchaseCountCard(id, userId);
        return ResponseEntity.ok(success);
    }

    /**
     * 用户使用次数权益卡
     */
    @PostMapping("/{id}/use")
    public ResponseEntity<Boolean> useCountCard(@PathVariable Long id, @RequestParam Long userId) {
        boolean success = countCardService.useCountCard(id, userId);
        return ResponseEntity.ok(success);
    }

    /**
     * 查询用户剩余次数
     */
    @GetMapping("/{id}/remaining-count")
    public ResponseEntity<Integer> getRemainingCount(@PathVariable Long id, @RequestParam Long userId) {
        int remainingCount = countCardService.getRemainingCount(id, userId);
        return ResponseEntity.ok(remainingCount);
    }

}